package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.SetmealDetails;
import com.itheima.service.SetmealDetailsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 套餐详情管理
 */

@RestController
@RequestMapping("/setmeal1")
public class SetmealDetailsController {
    @Reference//查找服务
    private SetmealDetailsService setmealDetailsService;

    //新增预约
    @RequestMapping("/add")
    public Result add(@RequestBody SetmealDetails setmealDetails){
        try{
            setmealDetailsService.add(setmealDetails);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealDetailsService.pageQuery(queryPageBean);
        return pageResult;
    }

    //删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            setmealDetailsService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    //编辑检查项
    @RequestMapping("/edit")
    public Result edit(@RequestBody SetmealDetails setmealDetails){
        try{
            setmealDetailsService.edit(setmealDetails);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
    //通过ID查询一条套餐的详细信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            SetmealDetails setmealDetails = setmealDetailsService.findById(id);
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,setmealDetails);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<SetmealDetails> list = setmealDetailsService.findAll();
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }
    @RequestMapping("/update")
    public Result updateOrderStatus(@RequestBody SetmealDetails setmealDetails){
        try{
            setmealDetailsService.updateOrderStatus(setmealDetails);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.UPDATE_orderStatus_FAIL);
        }
        return  new Result(true, MessageConstant.UPDATE_orderStatus_SUCCESS);
    }
}
